package com.czy;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Java操作Word {
    public static class Table {
        private String name;
        private List<PictureRenderData> img;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<PictureRenderData> getImg() {
            return img;
        }

        public void setImg(List<PictureRenderData> img) {
            this.img = img;
        }
    }

    public static void main(String[] args) throws IOException {
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();

        Configure config = Configure.builder()
                .bind("table", policy).build();

        XWPFTemplate template = XWPFTemplate.compile("f://template.docx", config).render(
                new HashMap<String, Object>() {{
                    put("enterpriseName", "企业名称");
                    put("checkNames", "人员");
                    put("patrolTime", "2024-01-01");
                    Table table = new Table();
                    table.setName("张三");
                    table.setImg(Arrays.asList(Pictures.ofStream(Files.newInputStream(Path.of("d://name.png")), PictureType.PNG).size(100, 100).create(), Pictures.ofStream(Files.newInputStream(Path.of("d://name.png")), PictureType.PNG).size(100, 100).create()));
                    List<Table> tables = new ArrayList<>();
                    tables.add(table);
                    put("table", tables);
                }});
        template.writeAndClose(new FileOutputStream("f://out_table.docx"));
        XWPFDocument xwpfDocument = new XWPFDocument(new FileInputStream("f://out_table.docx"));
    }
}
// 自定义表格
//                    TableRenderData tableRenderData = new TableRenderData();
//                    RowStyle rowStyle = new RowStyle();
//                    CellStyle cellStyle = new CellStyle();
//                    cellStyle.setVertAlign(XWPFTableCell.XWPFVertAlign.CENTER);
//
//                    ParagraphStyle paragraphStyle = new ParagraphStyle();
//                    paragraphStyle.setAlign(ParagraphAlignment.CENTER);
//                    cellStyle.setDefaultParagraphStyle(paragraphStyle);
//                    rowStyle.setDefaultCellStyle(cellStyle);
//
//
//                    RowRenderData rowRenderData = new RowRenderData();
//                    RowRenderData rowRenderData2 = new RowRenderData();
//
//                    rowRenderData.setRowStyle(rowStyle);
//                    rowRenderData2.setRowStyle(rowStyle);
//                    tableRenderData
//                            .addRow(rowRenderData
//                                    .addCell(new CellRenderData().addParagraph(new ParagraphRenderData().addText("11123123123")))
//                                    .addCell(new CellRenderData().addParagraph(new ParagraphRenderData().addPicture(Pictures.ofStream(Files.newInputStream(Path.of("d://name.png")), PictureType.PNG).size(100, 100).create())))
//                            )
//
//                            .addRow(rowRenderData2
//                                    .addCell(new CellRenderData().addParagraph(new ParagraphRenderData().addText("123")))
//                                    .addCell(new CellRenderData().addParagraph(new ParagraphRenderData().addPicture(Pictures.ofStream(Files.newInputStream(Path.of("d://name.png")), PictureType.PNG).size(100, 100).create())))
//                            );
//
//                    TableStyle tableStyle = new TableStyle();
//                    int[] ints = new int[2];
//                    ints[0] = 40;
//                    ints[1] = 60;
//                    tableStyle.setColWidths(ints);
//                    tableStyle.setWidth("100%");
//                    tableRenderData.setTableStyle(tableStyle);
//                    put("dangerMonth", tableRenderData);